const mysql = require('mysql2/promise');

// 数据库配置
const dbConfig = {
  host: 'localhost',
  user: 'root',
  password: '123456',
  database: 'chatime',
  port: 3306
};

async function testDynamicAvatars() {
  const pool = mysql.createPool(dbConfig);
  
  try {
    console.log('🔗 连接数据库...');
    await pool.getConnection();
    console.log('✅ 数据库连接成功');
    
    // 测试1: 检查动态列表API返回的数据
    console.log('\n📋 测试1: 检查动态列表数据');
    const [dynamics] = await pool.execute(`
      SELECT 
        d.id,
        d.user_id,
        u.name as user_name,
        u.avatar as user_avatar,
        d.content,
        d.like_count,
        d.comment_count,
        d.create_time
      FROM dynamics d
      LEFT JOIN users u ON d.user_id = u.id
      ORDER BY d.create_time DESC
      LIMIT 5
    `);
    
    console.log(`📊 找到 ${dynamics.length} 条动态`);
    dynamics.forEach((dynamic, index) => {
      console.log(`动态${index + 1}:`);
      console.log(`  用户: ${dynamic.user_name} (${dynamic.user_id})`);
      console.log(`  头像: ${dynamic.user_avatar || '无头像'}`);
      console.log(`  内容: ${dynamic.content.substring(0, 50)}...`);
    });
    
    // 测试2: 检查评论数据
    console.log('\n📋 测试2: 检查评论数据');
    if (dynamics.length > 0) {
      const dynamicId = dynamics[0].id;
      const [comments] = await pool.execute(`
        SELECT 
          c.id,
          c.user_id,
          u.name as user_name,
          u.avatar as user_avatar,
          c.content,
          c.create_time
        FROM dynamic_comments c
        LEFT JOIN users u ON c.user_id = u.id
        WHERE c.dynamic_id = ?
        ORDER BY c.create_time ASC
      `, [dynamicId]);
      
      console.log(`📊 动态 ${dynamicId} 有 ${comments.length} 条评论`);
      comments.forEach((comment, index) => {
        console.log(`评论${index + 1}:`);
        console.log(`  用户: ${comment.user_name} (${comment.user_id})`);
        console.log(`  头像: ${comment.user_avatar || '无头像'}`);
        console.log(`  内容: ${comment.content}`);
      });
    }
    
    // 测试3: 模拟完整的动态列表API调用
    console.log('\n📋 测试3: 模拟完整API调用');
    if (dynamics.length > 0) {
      const testUserId = dynamics[0].user_id;
      const [fullDynamicList] = await pool.execute(`
        SELECT 
          d.id,
          d.user_id,
          u.name as user_name,
          u.avatar as user_avatar,
          d.content,
          d.images,
          d.like_count,
          d.comment_count,
          d.create_time,
          CASE WHEN l.id IS NOT NULL THEN 1 ELSE 0 END as is_liked
        FROM dynamics d
        LEFT JOIN users u ON d.user_id = u.id
        LEFT JOIN dynamic_likes l ON d.id = l.dynamic_id AND l.user_id = ?
        ORDER BY d.create_time DESC
      `, [testUserId]);
      
      console.log(`📊 完整API返回 ${fullDynamicList.length} 条动态`);
      fullDynamicList.forEach((dynamic, index) => {
        console.log(`动态${index + 1}:`);
        console.log(`  用户: ${dynamic.user_name} (${dynamic.user_id})`);
        console.log(`  头像: ${dynamic.user_avatar || '无头像'}`);
        console.log(`  已点赞: ${dynamic.is_liked ? '是' : '否'}`);
      });
    }
    
    // 测试4: 检查头像分配情况
    console.log('\n📋 测试4: 检查头像分配情况');
    const [userAvatars] = await pool.execute(`
      SELECT name, avatar FROM users ORDER BY name
    `);
    
    console.log('👥 用户头像分配情况：');
    userAvatars.forEach(user => {
      console.log(`  ${user.name}: ${user.avatar || '无头像'}`);
    });
    
    console.log('\n🎉 动态头像系统测试完成！');
    
  } catch (error) {
    console.error('❌ 测试失败:', error);
  } finally {
    await pool.end();
  }
}

// 运行测试
testDynamicAvatars(); 